<template>
  <div class="big-box">
    <div class="search-container">
      <el-form
        :inline="true"
        label-width="100px"
        class="subject"
        ref="queryFormRef"
        :model="queryParams"
      >
        <el-form-item label="科目" prop="subject">
          <el-input
            v-model="queryParams.subject"
            placeholder="科目"
            clearable
            style="width: 200px; margin-right: 20px"
        /></el-form-item>
        <el-form-item class="handleQuery">
          <el-button type="primary" @click="handleQuery"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="resetQuery"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" @click="openDialog()"
          ><i-ep-plus />新增</el-button
        >
      </template>
      <el-table
        v-loading="loading"
        ref="studyFormRef"
        :data="studyList"
        highlight-current-row
        border
      >
        <el-table-column label="科目" prop="subject" align="center" />
        <el-table-column label="内容" prop="content" align="center" />
        <!-- show-overflow-tooltip -->
        <!-- <el-table-column label="作文题目" align="center" prop="tile" /> -->
        <el-table-column fixed="right" label="操作" align="center" width="300">
          <template #default="scope">
            <!-- <el-button
              type="primary"
              link
              size="small"
              @click="openViewDialog(scope.row.id)"
              ><i-ep-view />查看</el-button
            > -->
            <el-button
              type="primary"
              link
              size="small"
              @click="handleDelete(scope.row.id)"
              ><i-ep-delete />删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.page"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>

    <!-- 新增弹窗 -->
    <el-dialog
      width="500px"
      @close="closeDialogs"
      title="新增学习指南"
      v-model="dialog.visible"
    >
      <el-form
        label-width="100px"
        :model="formData"
        ref="studyFormRef"
        :rules="rules"
      >
        <el-form-item label="学科" prop="subject">
          <el-input
            placeholder="请输入学科"
            type="subject"
            v-model="formData.subject"
          />
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input
            placeholder="请输入内容"
            type="content"
            v-model="formData.content"
          />
        </el-form-item>
        <!-- <el-form-item label="文件" prop="file"
          ><el-upload
            class="upload-demo"
            action="/api/learnPath/add"
            :show-file-list="false"
            :on-success="handleSuccess"
            :before-upload="beforeUpload"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload></el-form-item
        > -->
        <!-- <el-form label-width="100px" ref="studyFormRef">
          <el-form-item label="文件" prop="file">
            <el-upload
              v-model="formData.file"
              class="single-uploader"
              :show-file-list="false"
              :before-upload="handleBeforeUpload"
              :http-request="uploadFile"
            >
              <el-butto size="small" type="primary">点击上传</el-butto>
            </el-upload>
          </el-form-item>
        </el-form> -->
      </el-form>
      <!-- <el-form label-width="100px" /> -->
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit">提 交</el-button>
          <el-button @click="closeDialogs">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, defineProps } from "vue";
import { uploadFileApi } from "@/api/file";
import { UploadRawFile, UploadRequestOptions } from "element-plus";

const props = defineProps({
  modelValue: {
    type: String,
    default: "",
  },
});

const emit = defineEmits(["update:modelValue"]);
const imgUrl = useVModel(props, "modelValue", emit);

/**
 * 自定义图片上传
 *
 * @param options
 */
async function uploadFile(options: UploadRequestOptions): Promise<any> {
  const fileInfo = await uploadFileApi(options.file);
  console.log(options.file);
  console.log(fileInfo);

  const file = fileInfo.msg;
  console.log(file);
  formData.file = file;
  console.log(formData.image);
}

/**
 * 限制用户上传文件的格式和大小
 */
function handleBeforeUpload(file: UploadRawFile) {
  if (file.size > 2 * 1048 * 1048) {
    ElMessage.warning("上传图片不能大于2M");
    return false;
  }
  return true;
}

import {
  getstudyPage,
  getstudyMenuIds,
  addstudy,
  updatestudy,
  deletestudy,
  getUserIds,
} from "@/api/pathplan/study/index";
import {
  studyForms,
  studyQuery,
  studyPageVO,
  studyPageResult,
  UserPageVO,
} from "@/api/pathplan/study/type";

const value = ref();
// const channelList = ref([]);
const editDialog = ref();
const removeIds = ref([]); // 删除用户ID集合 用于批量删除
// const removeId = ref([]); // 删除用户ID集合 用于批量删除
const loading = ref(false);
const studyList = ref<studyPageVO[]>();
const userList = ref<UserPageVO[]>();
const total = ref(0);
const queryFormRef = ref(ElForm);
const queryParams = reactive<studyQuery>({
  page: 1,
  pageSize: 10,
});
const UserPageVOs = reactive<UserPageVO>({});

import { useUserStore } from "@/store/modules/user";
const { user } = useUserStore();
console.log(user);

//验证
const rules = reactive({
  subject: [
    {
      required: true,
      message: "请填写学科",
      trigger: "blur",
    },
  ],
  content: [
    {
      required: true,
      message: "请填写学科",
      trigger: "blur",
    },
  ],
});

/** 查询 */
function handleQuery() {
  loading.value = true;
  getstudyPage(queryParams)
    .then(({ data }) => {
      studyList.value = data.records;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}
/** 重置查询 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.page = 1;
  handleQuery();
}

/** 删除 */
function handleDelete(id?: string) {
  const userIds = [id || removeIds.value].join(",");
  if (!userIds) {
    ElMessage.warning("请勾选删除项");
    return;
  }

  ElMessageBox.confirm("确认删除?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    loading.value = true;
    deletestudy(userIds)
      .then(() => {
        ElMessage.success("删除成功");
        resetQuery();
      })
      .finally(() => (loading.value = false));
  });
}

onMounted(() => {
  handleQuery();
});

const studyFormRef = ref(ElForm);

const formData = reactive<studyForms>({
  id: "",
  subject: "",
  file: "",
  content: "",
});

/** 关闭新增修改表单弹窗 */
function closeDialogs() {
  dialog.visible = false;
  resetForm();
}
/** 关闭新增修改表单弹窗 */
function closeViewDialogs() {
  dialog.viewvisible = false;
  resetForm();
}

/** 重置表单 */
function resetForm() {
  studyFormRef.value.resetFields();
  studyFormRef.value.clearValidate();

  formData.id = "";
  formData.subject = "";
  formData.file = "";
  formData.content = "";
}

const dialog = reactive({
  title: "",
  visible: false,
});
/** 打开修改增加表单弹窗 */
function openDialog() {
  dialog.visible = true;
}
function ViewopenDialog(id?: string) {
  dialog.viewvisible = true;
  console.log(id.id);
  const ID = id.id;
  if (ID) {
    dialog.title = "修改讲座/活动信息";
    getActivitiesMenuIds(ID).then(({ data }) => {
      Object.assign(formData, data);
    });
  } else {
    dialog.title = "新增";
  }
}
/** 新增修改保存提交 */
function handleSubmit() {
  studyFormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      const studyId = formData.id;
      ElMessageBox.confirm("确认增加?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        addstudy(formData)
          .then(() => {
            ElMessage.success("新增成功");
            closeDialogs();
            resetQuery();
          })
          .finally(() => (loading.value = false));
      });
    }
  });
}
</script>

<style scoped lang="scss">
.form-container {
  display: flex;
  flex-wrap: wrap;
}

.handleQuery {
  margin-left: auto;
}
/* 根据实际情况调整样式 */
.upload-demo {
  display: inline-block;
}
</style>
